#region License
// RateViewModel.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Shammateh.Win.UI.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xoqal.Data;
    using Xoqal.Presentation.ViewModels;

    public class RateViewModel : ShammatehCrudViewModel<Entities.Rate>
    {
        #region Fields

        private readonly Data.IRateRepository rateRepository;
        private readonly Data.IResourceRepository resourceRepository;
        private readonly Data.IProjectRepository projectRepository;
        private Entities.Project filterProject;
        private Entities.Resource filterResource;
        private Entities.Project project;
        private Entities.Resource resource;
        private List<ListItemViewModel> projects;
        private List<ListItemViewModel> resources;
        
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RateViewModel" /> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        /// <param name="rateRepository">The rate repository.</param>
        /// <param name="resourceRepository">The resource repository.</param>
        /// <param name="projectRepository">The project repository.</param>
        public RateViewModel(
            IUnitOfWork unitOfWork, 
            Data.IRateRepository rateRepository,
            Data.IResourceRepository resourceRepository,
            Data.IProjectRepository projectRepository)
            : base(unitOfWork, rateRepository)
        {
            this.rateRepository = rateRepository;
            this.resourceRepository = resourceRepository;
            this.projectRepository = projectRepository;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the view title.
        /// </summary>
        /// <value>
        /// The view title.
        /// </value>
        public override string ViewTitle
        {
            get { return "نرخ"; }
        }

        /// <summary>
        /// Gets or sets the filter project.
        /// </summary>
        /// <value>
        /// The filter project.
        /// </value>
        public Entities.Project FilterProject
        {
            get
            {
                return this.filterProject;
            }

            set
            {
                if (this.filterProject != value)
                {
                    this.filterProject = value;
                    this.RaisePropertyChanged(() => this.FilterProject);
                    this.UpdateData();
                }
            }
        }

        /// <summary>
        /// Gets or sets the filter resource.
        /// </summary>
        /// <value>
        /// The filter resource.
        /// </value>
        public Entities.Resource FilterResource
        {
            get
            {
                return this.filterResource;
            }

            set
            {
                if (this.filterResource != value)
                {
                    this.filterResource = value;
                    this.RaisePropertyChanged(() => this.FilterResource);
                    this.UpdateData();
                }
            }
        }

        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>
        /// The project.
        /// </value>
        public Entities.Project Project
        {
            get
            {
                return this.project;
            }

            set
            {
                if (this.project != value)
                {
                    this.project = value;
                    this.RaisePropertyChanged(() => this.Project);
                }
            }
        }

        /// <summary>
        /// Gets or sets the resource.
        /// </summary>
        /// <value>
        /// The resource.
        /// </value>
        public Entities.Resource Resource
        {
            get
            {
                return this.resource;
            }

            set
            {
                if (this.resource != value)
                {
                    this.resource = value;
                    this.RaisePropertyChanged(() => this.Resource);
                }
            }
        }

        /// <summary>
        /// Gets or sets the projects.
        /// </summary>
        /// <value>
        /// The projects.
        /// </value>
        public List<ListItemViewModel> Projects
        {
            get
            {
                return this.projects;
            }

            set
            {
                if (this.projects != value)
                {
                    this.projects = value;
                    this.RaisePropertyChanged(() => this.Projects);
                }
            }
        }

        /// <summary>
        /// Gets or sets the resources.
        /// </summary>
        /// <value>
        /// The resources.
        /// </value>
        public List<ListItemViewModel> Resources
        {
            get
            {
                return this.resources;
            }

            set
            {
                if (this.resources != value)
                {
                    this.resources = value;
                    this.RaisePropertyChanged(() => this.Resources);
                }
            }
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Gets the query.
        /// </summary>
        /// <returns></returns>
        protected override IQueryable<Entities.Rate> GetQuery()
        {
            var query = this.rateRepository.Query;

            if (this.FilterProject != null)
            {
                query = query.Where(w => w.ProjectId == this.FilterProject.Id);
            }

            if (this.FilterResource != null)
            {
                query = query.Where(w => w.ResourceId == this.FilterResource.Id);
            }

            return query;
        }

        /// <summary>
        /// Called when refresh command execute.
        /// </summary>
        protected override void OnRefreshCommandExecute()
        {
            base.OnRefreshCommandExecute();
            this.LoadBasicInformtions();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Loads the basic information.
        /// </summary>
        private void LoadBasicInformtions()
        {
            this.Projects = this.projectRepository.GetAllItems().ToListItemViewModel(p => p.Title, true, "[انتخاب پروژه]").ToList();
            this.Resources = this.resourceRepository.GetAllItems().ToListItemViewModel(r => r.FullName, true, "[انتخاب فرد]").ToList();
        }

        #endregion
    }
}
